# -*- encoding:utf-8 -*-
# @Time: 2022/10/26 21:02
# @Author : moran
# @Email: moran@maqu.com
# @File: serializers.py
# @Software: PyCharm
from rest_framework.fields import SerializerMethodField
from rest_framework.serializers import ModelSerializer
from users.serializers import UserSerializer
from .models import *

class ClassesSerializer(ModelSerializer):
    memberDetail = SerializerMethodField()

    class Meta:
        model = Classes
        exclude = ['is_delete']

    def get_memberDetail(self, instance):
        # 分类
        members = UserSerializer(instance.member.all(), many=True).data  # 获取当前班级所有的用户数据，并序列化
        return {
            'teachers': [i['username'] for i in members if i['is_superuser'] or 1 in i['groups']],
            'students': [i['username'] for i in members if 2 in i['groups']],
        }

class CreateUserSerializer(ModelSerializer):
    class Meta:
        model = User
        fields = ['username','first_name','email']
        extra_kwargs = {
            'username': {'required': True},
            'email': {'required': True}
        }

